/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Clases;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author JULIAN
 */
public class EditarHorasExtra extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            
        HttpSession session_actual= request.getSession(true);

        String id= request.getParameter("empleado");
        String hsuma= request.getParameter("horas");

        String almacen = (String) session_actual.getAttribute("alma");


        PreparedStatement stconsulta = null;
        PreparedStatement stinserta = null;
        
        SQL_Consultas consulta = new SQL_Consultas();
        SQL_Inserciones inserta = new SQL_Inserciones();
        
        inserta.setHsuma(hsuma);
        inserta.setIdemp(id);
        
        consulta.setAlmacen(almacen);
        consulta.setId(id);
        
        stconsulta=consulta.ExisteEmpleado();
        ResultSet resultadoconsulta=stconsulta.executeQuery();
        
        if(!resultadoconsulta.next()){
           out.println("<html>");
           out.println("<title>ERROR</title>");
           out.println("<Body Bgcolor=#000000>");
           out.println("<p><font color=white>El empleado no existe o usted no tiene los suficientes permisos para editar este empleado</p></font>");
           out.println("<h1><a href=opcionesJP.jsp>Regresar</a></h1>");
           out.println("</html>");
           stconsulta.close();  
           resultadoconsulta.close();
           
                                     }
        else{

            stconsulta=consulta.GetExtrasEMP();
            resultadoconsulta=stconsulta.executeQuery();

              if(!resultadoconsulta.next()){
                  
                  stinserta=inserta.InsertExtras();
                  stinserta.executeUpdate();
                 
                                           }
                  else{            
                  
                    stinserta=inserta.UpdateExtras();
                    stinserta.executeUpdate();
                    
                      }
            out.println("<html>");
            out.println("<Body Bgcolor=#000000>");
            out.println("<title>Actualizacion de datos</title>");
            out.println("<p><font color=white>la actualizacion de las horas extra se ha realizado con exito</p></font>");
            out.println("<input type=button style=Position:Absolute; left:20%; top:30% value=Regresar onClick= window.location.href='opcionesJP.jsp'>");
          
            out.println("</html>");
            stconsulta.close();
            stinserta.close();
            resultadoconsulta.close();
            }          
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(EditarHorasExtra.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(EditarHorasExtra.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
